package com.ctsi.ssdc.service.impl;


import com.ctsi.ssdc.domain.LitemallCouponUserExample;
import com.ctsi.ssdc.util.CouponUserConstant;
import org.springframework.stereotype.Service;
import org.apache.commons.collections.CollectionUtils;
import com.ctsi.ssdc.domain.LitemallCouponUser;
import com.ctsi.ssdc.service.LitemallCouponUserService;
import com.ctsi.ssdc.repository.LitemallCouponUserRepository;
import org.apache.commons.lang3.StringUtils;

import java.time.ZonedDateTime;
import java.util.List;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import com.ctsi.ssdc.model.PageResult;
import org.springframework.data.domain.Pageable;
import com.github.pagehelper.PageHelper;
/**
 * Service Implementation for managing LitemallCouponUser.
 *
 * @author ctsi-biyi-generator
 *
 */
@Service
public class LitemallCouponUserServiceImpl 
	extends StrengthenBaseServiceImpl<LitemallCouponUserRepository, LitemallCouponUser, Integer, LitemallCouponUserExample>
	implements LitemallCouponUserService {

    @Autowired
    LitemallCouponUserRepository litemallCouponUserRepository;


    /**
    * GET  /litemallCouponUsers : get the litemallCouponUsers firstStringBaseColumn.
    */
    @Override
    public PageResult<LitemallCouponUser> findFirstStringColumn(String str,Pageable pageable){
        if (Objects.nonNull(pageable)) {
            PageHelper.startPage(pageable.getPageNumber() + 1, pageable.getPageSize());
        }
        LitemallCouponUserExample litemallCouponUserExample = new LitemallCouponUserExample();
        String orderBy = getPageOrderBy(pageable);
        if(StringUtils.isNotEmpty(orderBy)) {
            litemallCouponUserExample.setOrderByClause(orderBy);
        }
        if (StringUtils.isEmpty(str)){
            litemallCouponUserExample.createCriteria();
        } 
        List<LitemallCouponUser>  data = litemallCouponUserRepository.selectByExample(litemallCouponUserExample);

        long count = 0L;
        if (CollectionUtils.isNotEmpty(data)){
            count = litemallCouponUserRepository.countByExample(litemallCouponUserExample);
        }
        return new PageResult<LitemallCouponUser>(data,count,count);
    }

    @Override
    public Integer countUserAndCoupon(Integer userId, Integer couponId) {
        LitemallCouponUserExample example = new LitemallCouponUserExample();
        example.or().andUserIdEqualTo(userId).andCouponIdEqualTo(couponId).andDeletedEqualTo(false);
        return (int)litemallCouponUserRepository.countByExample(example);
    }

    @Override
    public void add(LitemallCouponUser couponUser) {
        couponUser.setAddTime(ZonedDateTime.now());
        couponUser.setUpdateTime(ZonedDateTime.now());
        litemallCouponUserRepository.insertSelective(couponUser);
    }

    @Override
    public LitemallCouponUser findById(Integer userCouponId) {
        return litemallCouponUserRepository.selectByPrimaryKey(userCouponId);
    }

    @Override
    public LitemallCouponUser queryOne(Integer userId, Integer couponId) {
        List<LitemallCouponUser> couponUserList = queryList(userId, couponId, CouponUserConstant.STATUS_USABLE, 1, 1, "add_time", "desc");
        if(couponUserList.size() == 0){
            return null;
        }
        return couponUserList.get(0);
    }

    @Override
    public List<LitemallCouponUser> queryAll(Integer userId) {
        return queryList(userId, null, CouponUserConstant.STATUS_USABLE, null, null, "add_time", "desc");
    }

    @Override
    public List<LitemallCouponUser> findByOid(Integer orderId) {
        LitemallCouponUserExample example = new LitemallCouponUserExample();
        example.or().andOrderIdEqualTo(orderId).andDeletedEqualTo(false);
        return litemallCouponUserRepository.selectByExample(example);
    }

    private String getPageOrderBy(Pageable page) {
        if(page!= null && page.getSort() != null) {
            StringBuilder sb = new StringBuilder();
            page.getSort().forEach(sort -> sb.append(sort.getProperty())
            .append(" ").append(sort.getDirection()).append(","));
            if(sb.length() > 1) {
                return (sb.substring(0,sb.length()-1));
             }
        }
        return null;
    }


    public List<LitemallCouponUser> queryList(Integer userId, Integer couponId, Short status, Integer page, Integer size, String sort, String order) {
        LitemallCouponUserExample example = new LitemallCouponUserExample();
        LitemallCouponUserExample.Criteria criteria = example.createCriteria();
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if(couponId != null){
            criteria.andCouponIdEqualTo(couponId);
        }
        if (status != null) {
            criteria.andStatusEqualTo(status);
        }
        criteria.andDeletedEqualTo(false);

        if (!org.springframework.util.StringUtils.isEmpty(sort) && !org.springframework.util.StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        if (!org.springframework.util.StringUtils.isEmpty(page) && !org.springframework.util.StringUtils.isEmpty(size)) {
            PageHelper.startPage(page, size);
        }

        return litemallCouponUserRepository.selectByExample(example);
    }

    @Override
    public Integer countCoupon(Integer couponId) {
        LitemallCouponUserExample example = new LitemallCouponUserExample();
        example.or().andCouponIdEqualTo(couponId).andDeletedEqualTo(false);
        return (int)litemallCouponUserRepository.countByExample(example);
    }
}
